package com.renhuikeji.pro.ala.activity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.jdsjlzx.ItemDecoration.GridItemDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.luck.picture.lib.PictureSelectionModel;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.compress.Luban;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.renhuikeji.pro.ala.R;
import com.renhuikeji.pro.ala.adapter.GridMeAdapter;
import com.renhuikeji.pro.ala.interf.DeleteInter;
import com.renhuikeji.pro.ala.views.PhotoPopupWindow;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ProblemActivity extends BaseActivity implements DeleteInter{

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.content_et)
    EditText contentEt;
    @BindView(R.id.showLength)
    TextView showLength;
    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    private GridMeAdapter mAdapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private ArrayList<LocalMedia> selectList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_problem);
        ButterKnife.bind(this);
    }


    private void initRcycle() {
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        mAdapter = new GridMeAdapter(this, this);
        selectList = new ArrayList<LocalMedia>();
        LocalMedia localMedia = new LocalMedia();
        localMedia.setPath("");
        selectList.add(localMedia);
        mAdapter.setDataList(selectList);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        GridItemDecoration divider = new GridItemDecoration.Builder(this)
                .setHorizontal(R.dimen.size_1dp)
//                .setVertical(R.dimen.radius_corner)
                .setColorResource(R.color.white)
                .build();
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(divider);
        recyclerView.setLoadMoreEnabled(false);
        recyclerView.setPullRefreshEnabled(false);
        recyclerView.refresh();
        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (TextUtils.isEmpty( mAdapter.getDataList().get(position).getPath().trim())) {
                    showBottomPopupWin();
                } else {
                    PictureSelector.create(ProblemActivity.this).externalPicturePreview(position, selectList);
                }
            }
        });
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                break;
            case R.id.tv_confirm:
                break;
        }
    }


    public void showBottomPopupWin() {
//        hideKeyboard();
        LayoutInflater mLayoutInfalter = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        LinearLayout mPopView = (LinearLayout) mLayoutInfalter.inflate(
                R.layout.bottom_photo_select_popupwindow, null);
        PhotoPopupWindow mPopupWin = new PhotoPopupWindow(this, mPopView);
        mPopupWin.setAnimationStyle(R.style.BottomPopupAnimation);
        mPopupWin.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
        mPopupWin.setOnSelectPhotoListener(new PhotoPopupWindow.SelectPhotoListener() {
            @Override
            public void onGallery(View v) {
                gotoPic(6,3,false,false,100);
            }

            @Override
            public void onCamera(View v) {
                gotoCamer(101);
            }
        });
    }
    public void gotoPic(int max, int spancount, boolean crop, boolean cropCircle, int requesCode) {
        // 进入相册 以下是例子：用不到的api可以不写
//        if(!selectList3.isEmpty()){
//            for(LocalMedia bean: selectList3){
//                if(TextUtils.isEmpty(bean.getPath())){
//                    selectList3.remove(bean);
//                }
//            }
//        }
        PictureSelectionModel pictureModel = PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()
//                .theme()//主题样式(不设置为默认样式) 也可参考demo values/styles下 例如：R.style.picture.white.style
                .maxSelectNum(max)// 最大图片选择数量 int
                .minSelectNum(0)// 最小选择数量 int
                .imageSpanCount(spancount)// 每行显示个数 int
                .selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                .previewImage(true)// 是否可预览图片 true or false
                .enablePreviewAudio(false) // 是否可播放音频 true or false
                .compressGrade(Luban.THIRD_GEAR)// luban压缩档次，默认3档 Luban.THIRD_GEAR、Luban.FIRST_GEAR、Luban.CUSTOM_GEAR
                .isCamera(true)// 是否显示拍照按钮 true or false
                .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                .sizeMultiplier(0.5f)// glide 加载图片大小 0~1之间 如设置 .glideOverride()无效
                .setOutputCameraPath("/CustomPath")// 自定义拍照保存路径,可不填
                .enableCrop(crop)// 是否裁剪 true or false
                .compress(true)// 是否压缩 true or false
                .cropWH(20, 20)// 裁剪宽高比，设置如果大于图片本身宽高则无效 int
                .compressMode(PictureConfig.SYSTEM_COMPRESS_MODE)//系统自带 or 鲁班压缩 PictureConfig.SYSTEM_COMPRESS_MODE or LUBAN_COMPRESS_MODE
                .freeStyleCropEnabled(false)// 裁剪框是否可拖拽 true or false
                .circleDimmedLayer(cropCircle)// 是否圆形裁剪 true or false
                .showCropFrame(false)// 是否显示裁剪矩形边框 圆形裁剪时建议设为false   true or false
                .previewEggs(false);// 预览图片时 是否增强左右滑动图片体验(图片滑动一半即可看到上一张是否选中) true or false
//                if(flag==GRID_TYPE){
//                    pictureModel .selectionMedia(selectList3);// 是否传入已选图片 List<LocalMedia> list
//                }
        pictureModel.forResult(requesCode);//结果回调onActivityResult code
    }


    public void gotoCamer(int requesCode) {
        //包括裁剪和压缩后的缓存，要在上传成功后调用，注意：需要系统sd卡权限
//        PictureFileUtils.deleteCacheDirFile(MainActivity.this);
        PictureSelector.create(this)
                .openCamera(PictureMimeType.ofImage())
//                .openGallery(PictureMimeType.ofImage())
                .forResult(requesCode);
    }



    @Override
    public void delete(int positon) {

    }
}
